package gameClient.thread;

import java.util.*;

import com.alibaba.fastjson.JSON;
import gameClient.model.Message;
import gameClient.model.Poker;
import gameClient.model.PokerLabel;
import gameClient.view.ClientMainFrame;

public class ChuPaiThread extends Thread {
    private int time;
    private ClientMainFrame clientMainFrame;
    private boolean isRun;

    public int getTime() {
        return time;
    }
    public void setTime(int time) {
        this.time = time;
    }

    public ClientMainFrame getMainFrame() {
        return clientMainFrame;
    }
    public void setMainFrame(ClientMainFrame clientMainFrame) {
        this.clientMainFrame = clientMainFrame;
    }

    public boolean isRun() {
        return isRun;
    }
    public void setRun(boolean isRun) {
        this.isRun = isRun;
    }

    public ChuPaiThread(int time, ClientMainFrame clientMainFrame) {
        isRun = true;
        this.time = time;
        this.clientMainFrame = clientMainFrame;
    }

    public void run() {
        while (time >= 0 && isRun) {
            clientMainFrame.timeLabel.setText(String.valueOf(time));
            time--;
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        Message message = null;
        //如果是不出 时间到了/选择不出
        if (time == -1 || isRun == false && clientMainFrame.isOut == false) {
            //不出则隐藏出牌按钮
            clientMainFrame.chuPaiLabel.setVisible(false);
            clientMainFrame.buChuLabel.setVisible(false);
            clientMainFrame.timeLabel.setVisible(false);

            message = new Message(3, clientMainFrame.currentPlayer.getId(), "不出", null);

            //转换为json 交给 sendThread发送到服务器去
            String JsonMessage = JSON.toJSONString(message);
            clientMainFrame.sendThread.setMessage(JsonMessage);
        }

        //出牌
        if (isRun == false && clientMainFrame.isOut == true) {
            message = new Message(4, clientMainFrame.currentPlayer.getId(), "出牌", changePokerLabelToPoker(clientMainFrame.selectedPokerLabels));

            //转换为json并发到服务器
            String JsonMessage = JSON.toJSONString(message);
            clientMainFrame.sendThread.setMessage(JsonMessage);
            //将当前发送出去的扑克牌从扑克牌列表中移除
            clientMainFrame.removeOutPokerFromPokerList();

            //如果扑克列表的数量为0 代表赢了
            if (clientMainFrame.pokerLabels.size() == 0) {
                String finalMessageContent;
                if(clientMainFrame.isLord){
                    finalMessageContent = "lordWin";
                }else{
                    finalMessageContent = "farmerWin";
                }
                message = new Message(5, clientMainFrame.currentPlayer.getId(), finalMessageContent, null);
                JsonMessage = JSON.toJSONString(message);
                try {
                    Thread.sleep(100);
                    clientMainFrame.sendThread.setMessage(JsonMessage);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public List<Poker> changePokerLabelToPoker(List<PokerLabel> selectedPokerLabels) {
        List<Poker> pokerList = new ArrayList<>();
        for (int i = 0; i < selectedPokerLabels.size(); i++) {
            PokerLabel pokerLabel = selectedPokerLabels.get(i);
            Poker poker = new Poker(pokerLabel.getId(), pokerLabel.getName(), pokerLabel.getNum());
            pokerList.add(poker);
        }
        return pokerList;
    }


}

